#include "mainwindow.h"

#include <QtCore/QCoreApplication>
#include "loginwidget/loginwidget.h"
#include "commondef.h"
#include <QDebug>
#include "global/myresource.h"
#include "global/globalvar.h"
#include "mainwidget/mainwidget.h"
#include "game/gameview.h"
#include "game/gamescene.h"
#include "otherwidget/QProgressIndicator.h"
#include "otherwidget/endwidget.h"
#include <QPropertyAnimation>
#include "otherwidget/helpwidget.h"
#include "otherwidget/statisticswidget.h"
#include "otherwidget/aboutwidget.h"
#include "otherwidget/settingwidget.h"
#include <QEvent>

#define PI_W            50

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow           (parent),
      m_loginwidget         (NULL),
      m_mainwidget          (NULL),
      m_gameview            (NULL),
      m_pi                  (NULL)
{
    QPalette palette = this->palette();
    palette.setColor(QPalette::Background, QColor(193,210,240));//QColor(255, 192, 203)
    setPalette(palette);

    init();
}

MainWindow::~MainWindow()
{
}

void MainWindow::setOrientation(ScreenOrientation orientation)
{
#if defined(Q_OS_SYMBIAN)
    // If the version of Qt on the device is < 4.7.2, that attribute won't work
    if (orientation != ScreenOrientationAuto) {
        const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
        if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
            qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
            return;
        }
    }
#endif // Q_OS_SYMBIAN

    Qt::WidgetAttribute attribute;
    switch (orientation) {
#if QT_VERSION < 0x040702
    // Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
    case ScreenOrientationLockPortrait:
        attribute = static_cast<Qt::WidgetAttribute>(128);
        break;
    case ScreenOrientationLockLandscape:
        attribute = static_cast<Qt::WidgetAttribute>(129);
        break;
    default:
    case ScreenOrientationAuto:
        attribute = static_cast<Qt::WidgetAttribute>(130);
        break;
#else // QT_VERSION < 0x040702
    case ScreenOrientationLockPortrait:
        attribute = Qt::WA_LockPortraitOrientation;
        break;
    case ScreenOrientationLockLandscape:
        attribute = Qt::WA_LockLandscapeOrientation;
        break;
    default:
    case ScreenOrientationAuto:
        attribute = Qt::WA_AutoOrientation;
        break;
#endif // QT_VERSION < 0x040702
    };
    setAttribute(attribute, true);
}

void MainWindow::init()
{
    MyResource::initImage();

    globalvar::m_strfresh = tr("freshman");
    globalvar::m_strnormal = tr("general");
    globalvar::m_strmaster = tr("master");
    globalvar::m_strgun = tr("biggun");
    globalvar::m_bVoice = globalvar::IsVoice();

    if(globalvar::name() == "")
    {
        m_loginwidget = new LoginWidget(this);
        m_loginwidget->setGeometry(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
        connect(m_loginwidget,SIGNAL(tomain()),this,SLOT(starttomain()));
    }
    else
    {
        m_mainwidget = new MainWidget(this);
        m_mainwidget->setGeometry(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
        m_mainwidget->ShowContinueBtn();
        setMainConnect();
    }

    m_gameview = new GameView(this);
    m_gameview->setGeometry(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);  
    m_gameview->setVisible(false);

    m_gamescene = new gamescene(this);
    m_gameview->setScene(m_gamescene);
    connect(m_gamescene,SIGNAL(gameback()),this,SLOT(gameback()));
    connect(m_gamescene,SIGNAL(showhelp()),this,SLOT(ShowHelpWidget()));

    connect(&m_sudokuThread,SIGNAL(SudokuThreadOver()),this,SLOT(SudokuCreat()));

    m_endwidget = new EndWidget(this);
    m_endwidget->setGeometry(WINDOW_WIDTH,0,WINDOW_WIDTH,WINDOW_HEIGHT);
    m_endwidget->setVisible(false);
    connect(m_endwidget,SIGNAL(continueGame()),this,SLOT(ContinueGame()));
    connect(m_endwidget,SIGNAL(Tomenu()),this,SLOT(EndToMain()));

    globalvar::m_mainwindow = this;

    m_helpWidget = new HelpWidget(this);
    m_helpWidget->setGeometry(WINDOW_WIDTH,0,WINDOW_WIDTH,WINDOW_HEIGHT);
    connect(m_helpWidget,SIGNAL(hideHelpWidget()),this,SLOT(HideHelpWedget()));

    m_statisWidget = new StatisticsWidget(this);
    m_statisWidget->setGeometry(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
    m_statisWidget->setVisible(false);
    connect(m_statisWidget,SIGNAL(btom()),SLOT(statisTomain()));

    m_aboutWidget = new AboutWidget(this);
    m_aboutWidget->setGeometry(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
    m_aboutWidget->setVisible(false);
    connect(m_aboutWidget,SIGNAL(backtomain()),SLOT(aboutToMain()));

    m_setWidget = new SettingWidget(this);
    m_setWidget->setGeometry(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
    m_setWidget->setVisible(false);
    connect(m_setWidget,SIGNAL(stom()),this,SLOT(SetToMain()));


//    globalvar::setCompleteNum(0);
//    globalvar::setLevel(NEW_L);
}

void MainWindow::starttomain()
{
    if(m_loginwidget != NULL)
    {
        delete m_loginwidget;
        m_loginwidget = NULL;
    }

    if(m_mainwidget == NULL)
    {
        m_mainwidget = new MainWidget(this);
        m_mainwidget->setGeometry(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
        m_mainwidget->ShowContinueBtn();
        m_mainwidget->showFullScreen();
        setMainConnect();
    }
    else
    {
        m_mainwidget->ShowContinueBtn();
        m_mainwidget->showFullScreen();
    }
}

void MainWindow::playgame()
{
    m_mainwidget->setVisible(false);

    if(m_pi == NULL)
    {
        m_pi = new QProgressIndicator(this);
        m_pi->setWindowModality(Qt::WindowModal);
        m_pi->raise();
        m_pi->setGeometry((WINDOW_WIDTH - PI_W) / 2,
                          (WINDOW_HEIGHT - PI_W) / 2,
                          PI_W,PI_W);
        m_pi->startAnimation();
        m_pi->show();
    }

    m_sudokuThread.start();
}

void MainWindow::gameback()
{
    m_gameview->setVisible(false);
    m_mainwidget->ShowContinueBtn();
    m_mainwidget->setVisible(true);
}

void MainWindow::SudokuCreat()
{
    if(m_pi != NULL)
    {
        m_pi->stopAnimation();
        delete m_pi;
        m_pi = NULL;
    }

    m_gamescene->initGame();
    m_gameview->setVisible(true);
}

void MainWindow::GameWin()
{
    m_endwidget->setVisible(true);
    QPropertyAnimation *ani = new QPropertyAnimation(m_endwidget, "pos");
    ani->setDuration(1000);
    ani->setKeyValueAt(0,QPoint(WINDOW_WIDTH,0));
    ani->setKeyValueAt(0.6,QPoint(0,0));
    ani->setKeyValueAt(0.8,QPoint(20,0));
    ani->setKeyValueAt(1,QPoint(0,0));
    ani->start();
}

void MainWindow::HideHelpWedget()
{
    QPropertyAnimation *ani = new QPropertyAnimation(m_helpWidget, "pos");
    ani->setDuration(600);
    ani->setKeyValueAt(0,QPoint(0,0));
    ani->setKeyValueAt(1,QPoint(WINDOW_WIDTH,0));
    ani->start();

    m_gamescene->continueGame();
}

void MainWindow::ShowHelpWidget()
{
    QPropertyAnimation *ani = new QPropertyAnimation(m_helpWidget, "pos");
    ani->setDuration(1000);
    ani->setKeyValueAt(0,QPoint(WINDOW_WIDTH,0));
    ani->setKeyValueAt(0.6,QPoint(0,0));
    ani->setKeyValueAt(0.8,QPoint(20,0));
    ani->setKeyValueAt(1,QPoint(0,0));
    ani->start();
}

void MainWindow::statisTomain()
{
    m_statisWidget->setVisible(false);
    m_mainwidget->setVisible(true);
}

void MainWindow::maintoStatis()
{
    m_statisWidget->setStatisData();
    m_statisWidget->setVisible(true);
    m_mainwidget->setVisible(false);
}

void MainWindow::aboutToMain()
{
    m_aboutWidget->setVisible(false);
    m_mainwidget->setVisible(true);
}

void MainWindow::MainToAbout()
{
    m_mainwidget->setVisible(false);
    m_aboutWidget->setVisible(true);
}

void MainWindow::MainToSet()
{
    m_mainwidget->setVisible(false);
    m_setWidget->setVisible(true);
    m_setWidget->setEdit();
}

void MainWindow::SetToMain()
{
    m_setWidget->setVisible(false);
    m_mainwidget->setVisible(true);
}

void MainWindow::ContinueGame()
{
    m_endwidget->setVisible(false);
    m_gameview->setVisible(false);

    if(globalvar::levelDate(GUN_L) == "")
    {
        globalvar::iCurlevel = globalvar::level();
    }

    if(m_pi == NULL)
    {
        m_pi = new QProgressIndicator(this);
        m_pi->setWindowModality(Qt::WindowModal);
        m_pi->raise();
        m_pi->setGeometry((WINDOW_WIDTH - PI_W) / 2,
                          (WINDOW_HEIGHT - PI_W) / 2,
                          PI_W,PI_W);
        m_pi->startAnimation();
        m_pi->show();
    }

    m_sudokuThread.start();
}

void MainWindow::EndToMain()
{
    m_endwidget->setVisible(false);
    m_gameview->setVisible(false);
    m_mainwidget->ShowContinueBtn();
    m_mainwidget->setVisible(true);
}

void MainWindow::UnlockBtn(int l)
{
    m_mainwidget->Unlock(l);
}

void MainWindow::ContinuePlay()
{
    m_mainwidget->setVisible(false);
    m_gamescene->initGame(true);
    m_gameview->setVisible(true);
}

void MainWindow::setMainConnect()
{
    connect(m_mainwidget,SIGNAL(playgame()),this,SLOT(playgame()));
    connect(m_mainwidget,SIGNAL(maintoStatis()),this,SLOT(maintoStatis()));
    connect(m_mainwidget,SIGNAL(AboutToM()),this,SLOT(MainToAbout()));
    connect(m_mainwidget,SIGNAL(mtos()),this,SLOT(MainToSet()));
    connect(m_mainwidget,SIGNAL(continuegame()),this,SLOT(ContinuePlay()));
}

bool MainWindow::event(QEvent *event)
{
    if(event->type() == QEvent::ActivationChange)
    {
        if(m_gameview != NULL && m_aboutWidget != NULL)
        {
            if(m_gameview->isVisible() && (m_helpWidget->pos().x() == WINDOW_WIDTH)
                    && !m_endwidget->isVisible())
            {
                m_gamescene->pause();
            }
        }

        return true;
    }
    else
    {
        return QMainWindow::event(event);
    }
}


